package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.adpter.AppAboutAdpter;
import com.lanou.dllo.findfood.entity.AppAbout;
import com.lanou.dllo.findfood.entity.AppResult;
import com.lanou.dllo.findfood.tools.UpdateManger;
import com.lanou.dllo.findfood.util.AppdownListener;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/11/11.
 */
public class ApplicationActivity extends Activity implements AppdownListener {
    private RecyclerView recyclerView;
    private AppAboutAdpter adpter;
    private ImageView appback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app);
        initView();
        initData();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.appAbout);
        GridLayoutManager manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        adpter = new AppAboutAdpter();
        recyclerView.setAdapter(adpter);
        adpter.setListener(this);
        appback = (ImageView) findViewById(R.id.appback);
        appback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApplicationActivity.this.finish();
                overridePendingTransition(R.anim.push_left_in,
                        R.anim.push_left_out);

            }
        });

    }

    private void initData() {


        String url = "http://a1.go2yd.com/Website/data/friend-apps?platform=1&appid=food&cv=3.1." +
                "8&version=010911&net=wifi";
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                AppAbout about = gson.fromJson(response, AppAbout.class);
                if (about != null) {
                    List<AppResult> results = about.getResult();
                    if (results != null && results.size() > 0) {
                        adpter.addData(results);

                    }
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }
        };
        VolleySington.getInstance().addRequest(request);

    }

    private UpdateManger mUpdateManager;

    @Override
    public void appdown(AppResult about) {

        mUpdateManager = new UpdateManger(this, about.getLink());
        mUpdateManager.checkUpdateInfo();

    }
}
